package com.pat.jrgwt.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.pat.jrgwt.shared.MyConstants;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Jrgwt implements EntryPoint, ValueChangeHandler<String>
{
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad()
	{
        JrcEventsLog logs = JrcEventsLog.getInstance();
        RootPanel.get("dataQueueDiv").add(logs);
        logs.add("Starting JRC GWT application...");

        ValuesStorage storage = ValuesStorage.getInstance();
        storage.loadData();
        
        RootPanel.get("mainJrcDiv").add(Por3.getInstance());
	    
        if( false )
        {
            TabPanel p = new TabPanel();
            FlowPanel panel = new FlowPanel();
            panel.add(new PerformanceInPercent());
            //      panel.add(new Label("Presentation is under construction..."));
            p.add(panel, "Presentation");
            panel = new FlowPanel();
            panel.add(ProfitLossesView.getInstance());
            panel.add(PosSizeMultMinMove.getInstance());
            FxPipsView pipsView = FxPipsView.getInstance();
            pipsView.drawTable();
            panel.add(pipsView);
            panel.add(FxPositionSize.getInstance());
            FxWeighting fxWeigthView = FxWeighting.getInstance();
            fxWeigthView.drawTable();
            panel.add(fxWeigthView);
            ProfitLossesPerformance pfPerformance = ProfitLossesPerformance
                    .getInstance();
            pfPerformance.drawTable();
            panel.add(pfPerformance);
            //      panel.add(new PipFactor());
            //      panel.add(new PipFactorValues());
            p.add(panel, "Systeme PL");
            panel = new FlowPanel();
            ArchivePerf ap = ArchivePerf.getInstance();
            ap.redraw();
            panel.add(ap);
            p.add(panel, "Archiv Perf Data");
            panel = new FlowPanel();
            panel.add(DvRatio.getInstance());
            //        panel.add(new Label("DV Sh So Ratio is under construction..."));
            p.add(panel, "DV Sh So Ratio");
            panel = new FlowPanel();
            final EuroConversion ec = EuroConversion.getInstance();
            panel.add(ec);
            //        panel.add(new Label("Conv Euro is under construction..."));
            p.add(panel, "Conv Euro");
            p.setSize("800px", "500px");
            p.selectTab(1);
            RootPanel.get("mainJrcDiv").add(p);
        }
        RootPanel.get("loadedDataIndicator").add(LoadedDataView.getInstance());
	    

	    
//	    JsonRequest.get(MyConstants.EXCHANGE_SERVER_URL + "test/getconveuro?callback=", new JsonRequestHandler()
//        {
//            
//            @Override
//            public void onRequestCompleted(JavaScriptObject jso)
//            {
//                if( jso == null )
//                {
//                    Window.alert("Error getting JSON, null object!");
//                    return;
//                }
////                Window.alert("Got JSON! ");
//                double[][] arConv = EuroConversion.parseAndStoreJson(jso);
//                if( arConv != null )
//                {
//                    ec.setData(arConv);
//                    ec.refreshGrid();
//                }
////                ec.EuroGrid.setText(3, 3, "666.777");
////                Window.alert("EuroGrid[3,3]: " + ec.EuroGrid.getText(3, 3));
//            }
//        });

//	    RootPanel.get().add(new DoubleBox());
	    
//		RootPanel.get().add(new ProfitLossesView());
		
		
//		RichTextArea richText = new RichTextArea();
//		RichTextToolbar toolbar = new RichTextToolbar(richText);
//		RootPanel.get().add(toolbar);
//		RootPanel.get().add(richText);

//		TabPanel tp = new TabPanel();
//		tp.add(new HTML("<p>Presentation is under construction...</p>"), "Presentation");
//		tp.add(new ProfitLossesView(), "PL systeme");
//		tp.selectTab(1);
//		RootPanel.get().add(tp);
		
//		HelloWorld hw = new HelloWorld();
//		Document.get().getBody().appendChild(hw.getElement());
//		hw.setName("Hello");
		
//		String token = History.getToken();
//		History.addValueChangeHandler(this);
//		History.newItem(token, true);
	}

	

    protected void onTestButtonClicked()
    {
        FxWeighting fxWeighting = FxWeighting.getInstance();
        EuroConversion euroConversion = EuroConversion.getInstance();
        double v = 0;
        double fxValue = 0, ecValue = 0;
        double perfDd = 100;
        
        fxValue = fxWeighting.getSafe(1, 3);
        GWT.log("Sum(EUR): " + fxValue);
        v = fxValue * perfDd; 
        
        fxValue = fxWeighting.getSafe(1, 6);
        ecValue = euroConversion.getRatio("EURUSD", 1);
        GWT.log("EuroConv EURUSD: " + ecValue);
        GWT.log("Sum(USD): " + fxValue);
        v += fxValue * perfDd / ecValue; 
        
        fxValue = fxWeighting.getSafe(1, 7);
        ecValue = euroConversion.getRatio("EURNZD", 1);
        GWT.log("AUD: " + fxValue);
        GWT.log("EuroConv EURNZD: " + ecValue);
        v += fxValue * perfDd / ecValue;
        
        fxValue = fxWeighting.getSafe(1, 8);
        ecValue = euroConversion.getRatio("EURGBP", 1);
        GWT.log("GBP: " + fxValue);
        GWT.log("EuroConv EURGBP: " + ecValue);
        v += fxValue * perfDd / ecValue;
        
        GWT.log("Total: " + v);
//        Window.alert("2010-12: " + ArchivePerf.getInstance().getValueBy(2010, 12, ArchivePerf.SumColumn));
    }



    @Override
	public void onValueChange(ValueChangeEvent<String> event)
	{
		String token = event.getValue();
		if( token.isEmpty() )
		{
			
		}else if( token.equals("login") )
		{
			
		}else
		{
			Window.alert("Unknown history token: " + token);
		}
	}

}
